package com.example.smarthomeyzy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.smarthomeyzy.R;
import com.example.smarthomeyzy.Adapter.VpAdapter;
import com.example.smarthomeyzy.fragment_device.AlarmFragment;
import com.example.smarthomeyzy.fragment_device.DeviceFragment;
import com.example.smarthomeyzy.fragment_device.MineFragment;

import java.util.ArrayList;
import java.util.List;


public class SmartActivity extends AppCompatActivity {

    ViewPager vpMain;// ViewPager组件
    RadioGroup radioGroup;
    VpAdapter vpAdapter;//ViewPager组件使用的适配器
    List<Fragment> list;
    DeviceFragment deviceFragment;
    AlarmFragment alarmFragment;
    MineFragment mineFragment;


    public RadioButton rbtn_device;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_smart);
        rbtn_device=findViewById(R.id.rbtn_device);
        rbtn_device.setOnClickListener(view -> {
            Intent intent = new Intent(SmartActivity.this, Fragment_device1_add.class);
            startActivityForResult(intent, 1);
        });
        initView();
    }
    private void initView() {
        vpMain= findViewById(R.id.vp_main);
        radioGroup = findViewById(R.id.rg_group);
        deviceFragment = new DeviceFragment();
        alarmFragment =new AlarmFragment();
        mineFragment = new MineFragment();
        list = new ArrayList<>();
        list.add(deviceFragment);
        list.add(alarmFragment);
        list.add(mineFragment);
//初始化适配器
        vpAdapter = new VpAdapter(getSupportFragmentManager(),list);
//将适配器与ViewPager进行绑定
        vpMain.setAdapter(vpAdapter);
//处理滑动事件
        vpMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                switch (position)
                {
                    case 0:
                        radioGroup.check(R.id.rbtn_device);
                        break;
                    case 1:
                        radioGroup.check(R.id.rbtn_alarm);
                        break;
                    case 2:
                        radioGroup.check(R.id.rbtn_mine);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
//按钮的事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i)
                {
                    case R.id.rbtn_device:
                        vpMain.setCurrentItem(0);
                        break;
                        case R.id.rbtn_alarm:
                            vpMain.setCurrentItem(1);
                            break;
                            case R.id.rbtn_mine:
                                vpMain.setCurrentItem(2);
                                break;
                }
            }
        });


    }
}